package com.yhz.openapi.web.controller;

import com.yhz.openapi.common.Const;
import com.yhz.openapi.common.ResponseResult;
import com.yhz.openapi.common.ResultTable;
import com.yhz.openapi.entity.AdminUser;
import com.yhz.openapi.services.AdminUserServices;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class AdminUserController {
    @Autowired
    private AdminUserServices userServices;


    @RequestMapping("/login")
    public ResponseResult login(String email, String password, HttpSession session) {
        AdminUser user = userServices.doLogin(email, password);
        if (user != null) {
            session.setAttribute(Const.IS_LOGIN, user);
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("用户名或密码不正确哦!");
    }


    @RequestMapping("/logout")
    public void logout(HttpSession session, HttpServletResponse response) throws IOException {
        session.invalidate();
        //设置手动进行重定向
        response.sendRedirect("/login.html");
    }

    @RequestMapping("/table")
    public ResultTable table(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             String realName,
                             String email,
                             Integer status) {
        return userServices.table(page,limit,realName,email,status);
    }
}
